package main

import (
	"fmt"
	"github.com/gomodule/redigo/redis"
)

type Redis struct {
	conn redis.Conn
}

func (r *Redis) NewConn(network, address string, options ...redis.DialOption) error {
	conn, err := redis.Dial(network, address, options...)
	if err == nil {
		r.conn = conn
	}
	return err
}

func (r *Redis) Close() {
	r.conn.Close()
}

func (r *Redis) SetString(key, value string) error {
	_, err := r.conn.Do("Set", key, value)
	return err
}
func (r *Redis) GetString(key string) (str string, err error) {
	str, err = redis.String(r.conn.Do("Get", key))
	return
}

func (r *Redis) SetHash(key, field string, value interface{}) error {
	_, err := r.conn.Do("HSet", key, field, value)
	return err
}

func (r *Redis) GetHash(key, field string) (str interface{}, err error) {
	str, err = redis.String(r.conn.Do("HGet", key, field))
	return
}

func (r *Redis) SetMultiHash(key string, fv ...interface{}) error {
	var args = make([]interface{}, 1)
	args[0] = key
	args = append(args, fv...)
	_, err := r.conn.Do("HMSet", args...)
	return err
}

func (r *Redis) GetMultiHash(key string, filed ...string) (value []string, err error) {
	var args = make([]interface{}, 1)
	args[0] = key
	for _, v := range filed {
		args = append(args, v)
	}
	value, err = redis.Strings(r.conn.Do("HMGet", args...))
	return
}

func (r *Redis) SetTimeout(key string, timeout int) error {
	_, err := r.conn.Do("expire", key, timeout)
	return err
}

func main() {
	r := Redis{}
	err := r.NewConn("tcp", "0.0.0.0:6379")
	if err != nil {
		fmt.Printf("connect to redis error: %v\n", err)
		return
	}
	defer r.Close()
	a, b := redis.Strings(r.conn.Do("HGETALL", "users"))
	fmt.Println(a)
	for _, v := range a {
		fmt.Printf("value: %v, type: %T\n", v, v)
	}
	fmt.Println(b)
	//testSetGet(&r)
	//testHSetHGet(&r)
	//testHMSetHMGet(&r)
}

func testSetGet(r *Redis) {
	err := r.SetString("test1", "asdasd")
	if err != nil {
		fmt.Printf("set test1 value asdasd error: %v\n", err)
		return
	}
	str, err := r.GetString("test1")
	if err != nil {
		fmt.Printf("get test1 value error: %v\n", err)
		return
	}
	fmt.Println(111, str)
}

func testHSetHGet(r *Redis) {
	err := r.SetHash("test2", "name", "tom")
	if err != nil {
		fmt.Printf("hset test2 value name=tom error: %v\n", err)
		return
	}

	err = r.SetHash("test2", "age", 18)
	if err != nil {
		fmt.Printf("hset test2 value age=18 error: %v\n", err)
		return
	}
	value, err := r.GetHash("test2", "age")
	if err != nil {
		fmt.Printf("hget test2 value age error: %v\n", err)
		return
	}
	fmt.Println(222, value)
	value, err = r.GetHash("test2", "name")
	if err != nil {
		fmt.Printf("hget test2 value name error: %v\n", err)
		return
	}
	fmt.Println(333, value)
}

func testHMSetHMGet(r *Redis) {
	err := r.SetMultiHash("test3", "name", "tom", "1", "2")
	if err != nil {
		fmt.Printf("hmset test3 value name=tom, 1=2 error: %v\n", err)
		return
	}
	value, err := r.GetMultiHash("test3", "name", "1")
	if err != nil {
		fmt.Printf("hmget test3 value name, 1 error: %v\n", err)
		return
	}
	fmt.Println(value)
}

func testDemo() {
	// 1、链接到redis
	conn, err := redis.Dial("tcp", "0.0.0.0:6379")
	if err != nil {
		fmt.Printf("connect to redis error: %v\n", err)
		return
	}
	defer conn.Close()

	// 写入数据
	_, err = conn.Do("Set", "name", "tom")
	if err != nil {
		fmt.Printf("set value error: %v\n", err)
		return
	}
	fmt.Println("Set ok")

	// 读取数据
	r, err := redis.String(conn.Do("Get", "name"))
	if err != nil {
		fmt.Printf("set value error: %v\n", err)
		return
	}
	fmt.Println()
	fmt.Printf("Read ok, name: %v\n", r)
}
